<?php
namespace Home\Controller;
use Think\Controller;

/**
 * 用户相关操作
 * @author demontf
 *
 */
//允许跨域访问
header('Access-Control-Allow-Origin: *');

class UserController extends Controller {

	public function __call($method, $args){
		if (method_exists('UserController', $method)){
			$this->$method;
		}else{
			$response['STATUS'] = "error";
			$response['ERRMSG'] = "无该方法";
			$response['RESULT'] = array();
			$this->ajaxReturn($response);
		}
	}
    
    /**
     * 发送手机验证码
     * @param phone 
     * 调用地址：/sign/index.php/Home/User/sendVertify
     * @return {"STATUS":"error|success","ERRMSG":"参数错误|超时|发送失败","RESULT":[]}
     */
    public function sendVertify(){
    		$phone =  I('param.phone/s',"");
    		$phone_flag = $phone."_flag";
    	
    		$time = S($phone_flag);
    		if (!empty($time) && $time-date("Y-m-d H:i:s")<120){
    			$response['STATUS'] = "error";
    			$response['ERRMSG'] = "请求过多，稍后再试";
    			$response['RESULT'] = array();
    			$this->ajaxReturn($response);
    		}
    		if(empty($phone)){
    			$response['STATUS'] = "error";
    			$response['ERRMSG'] = "参数错误";
    			$response['RESULT'] = array("phone"=>$phone);
    			$this->ajaxReturn($response);
    		}
//     			$res_phone=M('user')->where("phone='$phone'")->find();
//     			if(!empty($res_phone)){
//     				$response['STATUS'] = "error";
//     				$response['ERRMSG'] = "手机号已被注册";
//     				$response['RESULT'] = array("phone"=>$phone);
//     				$this->ajaxReturn($response);
//     			}
    			$code=rand(1000,9999);
    			$res=sendMsg($phone,"权限验证码为:".$code);
    			//发送成功
    			if($res){
    				$response['STATUS'] = "success";
    				S($phone,$code."",180);
    				S($phone_flag,date("Y-m-d H:i:s"),120);
    				$response['ERRMSG'] = "";
    				$response['RESULT'] = array();
    				$this->ajaxReturn($response);
    			}else{
    				$response['STATUS'] = "error";
    				$response['ERRMSG'] = "发送失败";
    				$response['RESULT'] = array();
    				$this->ajaxReturn($response);
    			}
    		
    }
    

    /**
     * 注册兼登录 报名
     * @param phone
     * @param vertify 手机验证码
     * 调用地址：/sign/index.php/Home/User/register
     * @return {"STATUS":"error|success","ERRMSG":"参数错误|两次密码不一致|验证码过期或错误|注册失败","RESULT":[]}
     */
    public function register(){
    		$phone = I('param.phone/s');
    		$vertify = I('param.vertify/s');
    		
    		
    		if(empty($phone) ||empty($vertify)){
    			$response['STATUS'] = "error";
    			$response['ERRMSG'] = "参数错误";
    			$response['RESULT'] = array();
    			$this->ajaxReturn($response);
    		}
    		$vertify_session=S($phone);
    		if(empty($vertify_session) || $vertify_session!==$vertify){
    			$response['STATUS'] = "error";
    			$response['ERRMSG'] = "验证码过期或错误";
    			$response['RESULT'] = array("vertify_session"=>$vertify_session,"vertify"=>$vertify);
    			$this->ajaxReturn($response);
    		}
    		$res_phone=M('user')->where("phone='$phone'")->find();
     	if(!empty($res_phone)){
    		  	$response['STATUS'] = "success";
  	 		$response['ERRMSG'] = "";
    		    	$response['RESULT'] = array("phone"=>$phone);
    		    	$this->ajaxReturn($response);
    		}
    		
    		$user['phone']=$phone;
    		$user['name']="牛逼的见友";
    		$user['create_time']=date('Y-m-d H:i:s');
    		$res=M('user')->add($user);
    		if($res){
    			$response['STATUS'] = "success";
    			$response['ERRMSG'] = "";
    			$response['RESULT'] = array("phone"=>$phone);
    			$this->ajaxReturn($response);
    		}else{
    			$response['STATUS'] = "error";
    			$response['ERRMSG'] = "注册失败";
    			$response['RESULT'] = array();
    			$this->ajaxReturn($response);
    		}
    }
    /**
     * 获取参与次数
     * @param phone
     * 调用地址：/sign/index.php/Home/User/getTimes
     * @return {"STATUS":"error|success","ERRMSG":"参数错误|两次密码不一致|验证码过期或错误|注册失败","RESULT":[]}
     */
    public function getTimes(){
	    	$phone = I('param.phone/s',"");
	    	if(empty($phone)){
	    		$response['STATUS'] = "error";
	    		$response['ERRMSG'] = "参数错误";
	    		$response['RESULT'] = array();
	    		$this->ajaxReturn($response);
	    	}
	    	$res_phone=M('user')->where("phone='$phone'")->find();
	    	$count=M('record')->where(array("user"=>$res_phone['id']))->count();
    		$res=M('record')->where(array("user"=>$res_phone['id']))->order("create_time desc")->limit(4)->select();
    		
    		$arr = array();
    		if(empty($res)){
    			for($i=1;$i<=4;$i++){
    				$arr[$i] = false;
    			}
    		}else{
    			$num = count($res)-1;
    			$term = $res[0]['term'];
    			$arr[$term] = true;
    			for($i=3;$i>=1;$i--){
    				$term = $term-1;
    				foreach ($res as $key=>$value){
    					if($value['term']==$term){
    						$arr[$term]=true;
    						break;
    					}
	    				$arr[$term]=false;
    				}
    			}
    		}
    		
    		$response['STATUS'] = "success";
    		$response['ERRMSG'] = "";
    		$response['RESULT'] = array("record"=>$arr,"count"=>$count);
    		$this->ajaxReturn($response);
    }
    /**
     * 签到
     * @param phone
     * @param aid 活动id
     * 调用地址：/sign/index.php/Home/User/sign
     * @return {"STATUS":"error|success","ERRMSG":"参数错误|两次密码不一致|验证码过期或错误|注册失败","RESULT":[]}
     */
    public function sign(){
    		$phone = I('param.phone/s',"");
    		$aid = I('param.aid/i',0);
    		
    		if(empty($phone) || empty($aid)){
    			$response['STATUS'] = "error";
    			$response['ERRMSG'] = "参数错误";
    			$response['RESULT'] = array();
    			$this->ajaxReturn($response);
    		}
    		
    		
    		$res_phone=M('user')->where("phone='$phone'")->find();
    		if(!empty($res_phone)){
    			$res_record=M('record')->where(array("user"=>$res_phone['id'],"activity"=>$aid))->find();
    			if(!empty($res_record)){
    				$response['STATUS'] = "error";
    				$response['ERRMSG'] = "已经报名";
    				$response['RESULT'] = array();
    				$this->ajaxReturn($response);
    			}
    		}
    		M('record')->where(array("user"=>$res_phone['id']))->find();
    		
    		$res_activity = M('activity')->where("id=$aid")->find();
    		if(!empty($res_phone) && !empty($res_activity)){
    			$record['user']=$res_phone['id'];
    			$record['activity']=$res_activity['id'];
    			$record['create_time']=date("Y-m-d H:i:s");
    			$record['term']=$res_activity['term'];
    			$res_record=M('record')->add($record);
    			if(!empty($res_record)){
    				$response['STATUS'] = "success";
    				$response['ERRMSG'] = "签到成功";
    				$response['RESULT'] = array("activity"=>$res_activity);
    				$this->ajaxReturn($response);
    			}
    		}
    		$response['STATUS'] = "error";
    		$response['ERRMSG'] = "请重新登录";
    		$response['RESULT'] = array("phone"=>$phone);
    		$this->ajaxReturn($response);
    }
}
